package com.led.datatype;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

import net.sf.json.JSONObject;

public class LedData {
	private static int IDX_ID = 1;
	private static int IDX_INFO = 2;
	private static int IDX_USER_NAME = 3;
	private static int IDX_DATAS = 4;
	
	int id;
	String info;
	String datas;
	String userName;
	int userId;
	public int getId() {
		return id;
	}

	public String getInfo() {
		return info;
	}

	public String getDatas() {
		return datas;
	}
	public String getUserName(){
		return userName;
	}
	public int getUserId(){
		return userId;
	}
	public LedData(String info, String inDatas, int userId){
		this.info = info;
		this.datas = inDatas.replaceAll("\t|\r|\n", " "); //
		this.datas = datas.replaceAll("[ ]+", " ");
		this.userId = userId;
	}
	public String toJsonStr(){
		JSONObject js = new JSONObject();
		js.put("id", id);
		js.put("info", info);
		js.put("datas", datas);
		js.put("userName", userName);
		return js.toString();
	}

	public LedData() {
		
	}
//	public LedData(JSONObject js) {
//		if(js.containsKey("id")){
//		   id = js.getInt("id");
//		}
//		info = js.getString("info");
//		datas = js.getString("datas");
//		userName = js.getString("userName");
//	}
//	
	@Override
	public String toString() {
		return toJsonStr();
	}
	
	public static LinkedList<LedData> getDatas(ResultSet rs){
		LinkedList<LedData> loadDatas = new LinkedList<LedData>();
		if(null == rs){
			return loadDatas;
		}
		
		try {
			while(rs.next()){
				LedData data = new LedData();
				data.id = rs.getInt(IDX_ID);
				data.info = rs.getString(IDX_INFO);
				data.userName = rs.getString(IDX_USER_NAME);
				data.datas = rs.getString(IDX_DATAS);
				loadDatas.add(data);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return loadDatas;
	}
	
}
